#include "com.h"

int main()
{
  umask(0);
  //创建管到文件
  if(mkfifo(MY_FILE, 0666) < 0)
  {
    perror("mkfifo");
    return 1;
  }

  //只需要文件操作就可以了
  int fd = open(MY_FILE, O_RDONLY);  
  if(fd < 0)
  {
    perror("open");
    return 1;
  }

  //业务逻辑
  while(1)
  {
    sleep(50);//休眠50秒
    char buffer[64] = {0};
    ssize_t s = read(fd, buffer, sizeof(buffer) - 1);
    if(s > 0)
    {
      // success
      buffer[s] = 0;
      printf("client say:%s\n", buffer);
    }
    else if(s == 0)
    {
      //peer close
      printf("client quit\n");
      break;
    }
    else 
    {
      //error
      perror("read");
      break;
    }
  }
  close(fd);
  return 0;
}
